package com.jeeplus.modules.sys.web;

import com.alibaba.fastjson.JSONObject;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.json.ReturnJson;
import com.jeeplus.common.utils.ResponseCode;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.entity.*;
import com.jeeplus.modules.sys.mapper.ApiTestMapper;
import com.jeeplus.modules.sys.service.ApiTestService;
import com.jeeplus.modules.test.one.dialog.entity.Leave1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * @author Administrator
 * @title: ApiController
 * @projectName jeeplus
 * @description: TODO
 * @date 2019/7/19:53
 */
@Api(value = "ApiController", description = "API控制器")
@Controller
@RequestMapping(value = "${apiPath}")
@CrossOrigin
public class ApiTestController extends BaseController {
    @Autowired
    ApiTestService apiService;
    private ResponseCode responseCode;
    @Autowired
    ApiTestMapper apiMapper;


    @ApiOperation(notes = "test",tags = {"test","test"}, httpMethod = "GET", value = "测试")
    @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "query",dataType = "string")})
    @RequestMapping(value = "test")
    @ResponseBody
    public ReturnJson getTest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //DataSourceContextHolder.setDbType("db2");
        ReturnJson ajaxJson = new ReturnJson();
        User user=new User();
        user.setNewPassword("test");
        System.out.println(beanValidatorList(user));
        System.out.println("test");
        ajaxJson.setCode(200);
        ajaxJson.setData(beanValidatorList(user));
        return ajaxJson;
    }
    @RequestMapping(value = "test1")
    @ResponseBody
    public ReturnJson getTest1(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //DataSourceContextHolder.setDbType("db2");
        ReturnJson ajaxJson = new ReturnJson();
        User user=new User();
        user.setNewPassword("test");
        System.out.println(beanValidatorList(user));
        System.out.println("test");
        ajaxJson.setCode(200);
        return ajaxJson;
    }
    @RequestMapping(value = "testapi/{t}/{api1}")
    @ResponseBody
    public AjaxJson testApi(@PathVariable(value = "t") String t, @PathVariable("api1") String api, HttpServletRequest request, HttpServletResponse response){
        AjaxJson ajax=new AjaxJson();
        ajax.setSuccess(true);
        ajax.setMsg(t+":-"+api);
        return ajax;
    }
    @RequestMapping(value = {"", "demo1"})
    public String demo1() {
        return "modules/sys/Api/demo";
    }
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject upload(@RequestParam("file") MultipartFile file, HttpServletRequest request ) {
        System.out.println("上传开始");
        JSONObject json = new JSONObject();
        json.put("code", "1");
        if( file.isEmpty() ) {
            json.put("msg", "上传文件为空");
            return json;
        }else {
            String savePath = request.getServletContext().getRealPath("/upload/");
            String fileName=file.getOriginalFilename();
            String pathname = savePath + fileName;
            File dest = new File(pathname);
            if( !dest.getParentFile().exists() ) {
                dest.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dest);
                json.put("code", 1);
                json.put("msg", "上传成功");
                json.put("imgPath", pathname);
                return json;
            } catch (Exception e) {
                json.put("msg", e.getMessage());
                return json;
            }
        }
    }
}
